package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.GameCateGoryDao;
import com.zwxbc.funplay.entity.GameCategory;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GameCategoryDaoImpl implements GameCateGoryDao {
	private static final String INSERT_CATEGORY = "INSERT INTO game_category (category_name) VALUES (?)";
	private static final String SELECT_CATEGORY_BY_ID = "SELECT * FROM game_category WHERE category_id = ?";
	private static final String SELECT_ALL_CATEGORIES = "SELECT * FROM game_category";
	private static final String UPDATE_CATEGORY = "UPDATE game_category SET category_name = ? WHERE category_id = ?";
	private static final String DELETE_CATEGORY = "DELETE FROM game_category WHERE category_id = ?";

	@Override
	public int addGameCategory(GameCategory category) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CATEGORY)) {
			preparedStatement.setString(1, category.getCategoryName());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public GameCategory getGameCategoryById(Long categoryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_CATEGORY_BY_ID)) {
			preparedStatement.setLong(1, categoryId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createCategoryFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<GameCategory> getAllGameCategories() {
		List<GameCategory> categories = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_CATEGORIES);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				categories.add(createCategoryFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return categories;
	}

	@Override
	public int updateGameCategory(GameCategory category) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CATEGORY)) {
			preparedStatement.setString(1, category.getCategoryName());
			preparedStatement.setLong(2, category.getCategoryId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteGameCategory(Long categoryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_CATEGORY)) {
			preparedStatement.setLong(1, categoryId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 GameCategory 对象
	private GameCategory createCategoryFromResultSet(ResultSet resultSet) throws SQLException {
		GameCategory category = new GameCategory();
		category.setCategoryId(resultSet.getLong("category_id"));
		category.setCategoryName(resultSet.getString("category_name"));
		return category;
	}
}
